package com.ruoyi.energy.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.energy.domain.DeviceMaintain;
import com.ruoyi.energy.mapper.DeviceMaintainMapper;
import com.ruoyi.energy.service.IDeviceMaintainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备维修Service业务层处理
 *
 * @author cjie
 * @date 2022-01-04
 */
@Service
public class DeviceMaintainServiceImpl implements IDeviceMaintainService {
    @Autowired
    private DeviceMaintainMapper deviceMaintainMapper;

    /**
     * 查询设备维修
     *
     * @param id 设备维修ID
     * @return 设备维修
     */
    @Override
    public DeviceMaintain selectDeviceMaintainById(String id) {
        DeviceMaintain result = deviceMaintainMapper.selectDeviceMaintainById(id);
//        if(result.getModuleType().equals("电梯")){
//            if(StringUtils.isBlank(result.getDwCode())){
//
//            }
//        }
        return result;
    }

    /**
     * 查询设备维修列表
     *
     * @param deviceMaintain 设备维修
     * @return 设备维修
     */
    @Override
    public List<DeviceMaintain> selectDeviceMaintainList(DeviceMaintain deviceMaintain) {
        List<DeviceMaintain> results = null;
//        if(deviceMaintain.getModuleType() != null && deviceMaintain.getModuleType().equals("电梯")){
//            results = deviceMaintainMapper.selectDeviceMaintainListWithElevator(deviceMaintain);
//        }else{
//            results = deviceMaintainMapper.selectDeviceMaintainList(deviceMaintain);
//        }

        results = deviceMaintainMapper.selectDeviceMaintainList(deviceMaintain);

        return results;
    }

    /**
     * 新增设备维修
     *
     * @param deviceMaintain 设备维修
     * @return 结果
     */
    @Override
    public int insertDeviceMaintain(DeviceMaintain deviceMaintain) {
        deviceMaintain.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        deviceMaintain.setCreateTime(DateUtils.getNowDate());
        deviceMaintain.setCreateBy(SecurityUtils.getUsername());
        //根据deviceid,moduleType获取信息，插入。
        return deviceMaintainMapper.insertDeviceMaintain(deviceMaintain);
    }

    /**
     * 修改设备维修
     *
     * @param deviceMaintain 设备维修
     * @return 结果
     */
    @Override
    public int updateDeviceMaintain(DeviceMaintain deviceMaintain) {
        deviceMaintain.setUpdateTime(DateUtils.getNowDate());
        deviceMaintain.setUpdateBy(SecurityUtils.getUsername());
        return deviceMaintainMapper.updateDeviceMaintain(deviceMaintain);
    }

    /**
     * 批量删除设备维修
     *
     * @param ids 需要删除的设备维修ID
     * @return 结果
     */
    @Override
    public int deleteDeviceMaintainByIds(String[] ids) {
        return deviceMaintainMapper.deleteDeviceMaintainByIds(ids);
    }

    /**
     * 删除设备维修信息
     *
     * @param id 设备维修ID
     * @return 结果
     */
    @Override
    public int deleteDeviceMaintainById(String id) {
        return deviceMaintainMapper.deleteDeviceMaintainById(id);
    }

    @Override
    public Map<String, Integer> selectTransRepairByMonth(String month) {
        Map<String, Integer> resultMap = new LinkedHashMap<>();
        List<String> list = deviceMaintainMapper.selectRepairByMonth(month);
        for(String repair:list){
            if (resultMap.containsKey(repair)){
                resultMap.put(repair,resultMap.get(repair)+1);
            }else {
                resultMap.put(repair,1);
            }
        }
        return resultMap;
    }

    @Override
    public Map<String, Integer> getTypeRatio() {
        //todo 能源设备统计，设备故障，设备维修，能耗报警
        Map<String, Integer> resultMap = new LinkedHashMap<>();
        //设备运行日志
        resultMap.put("warning",0);
        //设备状态
        resultMap.put("fault",3);
        // omp_device_maintain 统计能源修复记录
        resultMap.put("repairing",2);
        return resultMap;
    }
}
